// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial

import { CupertinoFontSettings, CupertinoPalette } from "styling.slint";
import { FocusBorder } from "components.slint";
import { LineEditBase} from "../common/lineedit-base.slint";

export component LineEdit {
    in property <bool> enabled <=> i-base.enabled;
    in property <InputType> input-type <=> i-base.input-type;
    in property <TextHorizontalAlignment> horizontal-alignment <=> i-base.horizontal-alignment;
    in property <bool> read-only <=> i-base.read-only;
    in property <length> font-size <=> i-base.font-size;
    in property <string> placeholder-text <=> i-base.placeholder-text;
    out property <bool> has-focus <=> i-base.has-focus;
    in-out property <string> text <=> i-base.text;

    callback accepted <=> i-base.accepted;
    callback edited <=> i-base.edited;

    public function select-all() {
        i-base.select-all();
    }

    public function clear-selection() {
        i-base.clear-selection();
    }

    public function cut() {
        i-base.cut();
    }

    public function copy() {
        i-base.copy();
    }

    public function paste() {
        i-base.paste();
    }

    vertical-stretch: 0;
    horizontal-stretch: 1;
    min-width: max(160px, i-layout.min-width);
    min-height: max(22px, i-layout.min-height);
    forward-focus: i-base;

    states [
        disabled when !root.enabled : {
            i-base.text-color: CupertinoPalette.foreground-secondary;
            i-base.placeholder-color: CupertinoPalette.foreground-secondary;
            i-background.background: CupertinoPalette.tertiary-control-background;
        }
        focused when root.has-focus : {
            i-background.background: CupertinoPalette.control-background;
        }
    ]

    FocusBorder {
        x: (parent.width - self.width) / 2;
        y: (parent.height - self.height) / 2;
        width: parent.width + 6px;
        height: parent.height + 6px;
        has-focus: root.has-focus;
    }

    i-background := Rectangle {
        background: CupertinoPalette.alternate-background;
        border-color: CupertinoPalette.border;
        border-width: 1px;
    }

    i-layout := HorizontalLayout {
        padding-left: 7px;
        padding-right: 7px;

        i-base := LineEditBase {
            font-size: CupertinoFontSettings.body.font-size;
            font-weight: CupertinoFontSettings.body.font-weight;
            selection-background-color: CupertinoPalette.selection-background;
            selection-foreground-color: CupertinoPalette.selection-foreground;
            text-color: CupertinoPalette.foreground;
            margin: i-layout.padding-left + i-layout.padding-right;
            placeholder-color: CupertinoPalette.foreground-secondary;
        }
    }
}
